function d = mahalanobis(varargin)
param = varargin;
Y = param{1};
ny = size(Y, 1);
if length(param) == 2
    X = param{2};
    [Cx, mx] = covmatrix(X);
elseif length(param) == 3
    Cx = param{2};
    mx = param{3};
else
    error('Wrong number of inputs.')
end
mx = mx(:)';
Yc = Y - mx(ones(ny, 1), :);
d = real(sum(Yc/Cx.*conj(Yc), 2));
end
